package ua.exchangeratesukraine.actions;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import ua.exchangeratesukraine.R;
import android.content.Context;
import android.util.Log;

public class NetRequest {

	private final String	LOG_TAG	= getClass().getSimpleName();

	public NetRequest() {
	}

	public String getResponseString(Context context, String urlString) {
		InputStream response = null;
		StringBuilder sb = new StringBuilder();
		HttpURLConnection connection = null;

		URL url = null;
		try {
			url = new URL(urlString);
			connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");
			connection.connect();
			response = connection.getInputStream();
			InputStreamReader in = new InputStreamReader(response, "windows-1251");
			BufferedReader br = new BufferedReader(in);
			String line = "";
			while ((line = br.readLine()) != null) {
				sb.append(line + "\n");
			}
			br.close();
			return sb.toString();

		} catch (MalformedURLException e) {

			Log.d(LOG_TAG, "error : " + e.getMessage());
			return context.getString(R.string.text_toast_url_connection_error);
		} catch (IOException e) {

			Log.d(LOG_TAG, "error : " + e.getMessage());
			return context.getString(R.string.text_toast_connection_error);
		} finally {

			connection.disconnect();
		}
	}
}